// 邻接矩阵
#include <stdio.h>
#include <stdlib.h>

#define MaxVertex 5

typedef char E;

typedef struct MatrixGraph{
    // 顶点数
    int vertexCount;
    // 边数
    int edgeCount;
    // 矩阵
    int matrix[MaxVertex][MaxVertex];
    // 每一个顶点对应的数组
    E data[MaxVertex];
}* Graph;

// 创建图
Graph create() {
    Graph graph = malloc(sizeof(struct MatrixGraph));
    // 初始状态下，顶点和边数均为0
    graph->vertexCount = 0;
    graph->edgeCount = 0;
    // 将矩阵也初始为0
    for (int i = 0; i < MaxVertex; ++i) {
        for (int j = 0; j < MaxVertex; ++j) {
            graph->matrix[i][j] = 0;
        }
    }
    return graph;
}

// 添加顶点
void addVertex(Graph graph, E element) {
    // 已满则无法再添加
    if(graph->vertexCount >= MaxVertex) return;
    // 未满可以添加
    graph->data[graph->vertexCount++] = element;
}

// 添加从 ？ 到 ？ 的边
void addEdge(Graph graph, int from, int to) {
    if(graph->matrix[from][to] == 0) {
        // 若是无向图，还有另一侧也需要更新
        graph->matrix[from][to] = 1;
        graph->edgeCount++;
    }
}

void printGraph(Graph graph){
    for (int i = -1; i < graph->vertexCount; ++i) {
        for (int j = -1; j < graph->vertexCount; ++j) {
            if(j == -1)
                printf("%c", 'A' + i);
            else if(i == -1)
                printf("%3c", 'A' + j);
            else
                printf("%3d", graph->matrix[i][j]);
        }
        putchar('\n');
    }
}



int main() {
    Graph graph = create();
    for(int i = 'A'; i <= 'D'; i++) {
        addVertex(graph, (char)i);
    }
    addEdge(graph, 0, 1);   //A -> B
    addEdge(graph, 1, 2);   //B -> C
    addEdge(graph, 2, 3);   //C -> D
    addEdge(graph, 3, 0);   //D -> A
    addEdge(graph, 2, 0);   //C -> A
    printGraph(graph);
}